package fr.mystnihon.game.model.graphic;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

/**
 * J'ai plusieur s animation que je charge dans une liste Un acteur ou monstre a
 * un sprite (composition).
 * 
 * @author Lévi
 * 
 */
public class Sprite {

	protected Animation anim;
	protected Bitmap bitmap;
	public int x;
	public int y;
	long frameTicker = 0L;
	int framePeriod = 1000 / 30;
	int currentFrame;
	private float veloX = 0;
	private float veloY = 0;
	private Rect sourceRect; // Area selected on the bitmap
	private Rect destRect; // Area where to draw on the device screen
	private Rect extraBound; // Dimension of the sprite.
	private boolean isVisible;

	public Sprite() {
		this(null);
	}

	public Sprite(Animation animation) {
		anim = animation;
		currentFrame = 0;
		isVisible = true;
	}

	public void update(long elapsedTime) {
		x += veloX * elapsedTime;
		y += veloY * elapsedTime;
		anim.update(elapsedTime);

	}

	public void draw(Canvas canvas) {
		// where to draw the sprite
		sourceRect = getDrawRectangle();
		destRect = new Rect(getX(), getY(), getX() + getWidth(), getY()
				+ getHeight());
		canvas.drawBitmap(getBitmap(), sourceRect, destRect, null);
	}

	public final int getX() {
		return x;
	}

	public final int getY() {
		return y;
	}

	public final int getIntX() {
		return (int) x;
	}

	public final int getIntY() {
		return (int) y;
	}

	public final void setX(int x) {
		this.x = x;
	}

	public final void setY(int y) {
		this.y = y;
	}

	public final int getHeight() {
		return 32;
	}

	public final int getWidth() {
		return 32;
	}

	public final float getVelocityX() {
		return veloX;
	}

	public final float getVelocityY() {
		return veloY;
	}

	public final void setVelocityX(float veloX) {
		this.veloX = veloX;
	}

	public final void setVelocityY(float veloY) {
		this.veloY = veloY;
	}

	public final void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}

	public final Bitmap getBitmap() {
		return bitmap;
	}

	public final boolean isVisible() {
		return isVisible;
	}

	public final void setVisible(boolean visible) {
		isVisible = visible;
	}

	public final Rect getDrawRectangle() {
		return anim.getRectangle();
	}

	public final Rect getPosRectangle() {

		extraBound = new Rect(getIntX(), getIntY(), getIntX()
				+ getDrawRectangle().width(), getIntY()
				+ getDrawRectangle().height());

		return extraBound;
	}

	public final void setAnimation(Animation anim) {
		if (anim != null) {
			this.anim = anim;
		}
	}
}
